/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import olympicgameslogic.Edition;
import olympicgameslogic.LinkedList;

/**
 *
 * @author Fabio Mendonca
 */
public class DialogEditionsMain extends JFrame {

   private JPanel pNorte, pMod, pMbt, pCentro;
   private JScrollPane scroller;
   private JLabel nome;
   private JTable tabela1;
   private JButton newButton, deleteButton, editButton, manEventsButton;
   // private static int linha;
   private ClassLoader cldr = this.getClass().getClassLoader();
/// variaveis para a tabela subsituir por dados recebidos em array
   String[] columnNames = {"Year", "City", "Designation", "Delegations", "Events"};
   private LinkedList<Edition> editions;
   private DefaultTableModel TableModel;
   private int lineSelected;
   private Edition tempEdition;

   public DialogEditionsMain(String titulo) {
      super(titulo);
      super.setIconImage(Toolkit.getDefaultToolkit().getImage(cldr.getResource("images/extra/icon.png")));
      //  this.setLocation(pai.getX() + 100, pai.getY() + 100);
      setLayout(new BorderLayout());
      editions = OlympicGamesGui.ap.getEditions();
      pNorte = new JPanel(new FlowLayout(FlowLayout.CENTER));
      pNorte.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));

      nome = new JLabel();
      nome.setText("Editions");
      nome.setFont(new Font("Arial", Font.PLAIN + Font.BOLD, 20));
      ImageIcon img = new ImageIcon(cldr.getResource("images/extra/page_info.png"));
      nome.setIcon(img);
      pNorte.add(nome);
      pNorte.setBackground(Color.WHITE);
      add(pNorte, BorderLayout.NORTH);
      pMod = new JPanel(new BorderLayout());
      pMbt = new JPanel(new FlowLayout());
      newButton = new JButton("New", new ImageIcon(cldr.getResource("images/extra/page_add.png")));
      newButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    DialogManageEdition manEd = new DialogManageEdition(DialogEditionsMain.this, ("Create Edtition"), null, DialogEditionsMain.this);
	    manEd.showJan();


	 }
      });
      deleteButton = new JButton("Delete", new ImageIcon(cldr.getResource("images/extra/page_delete.png")));
      deleteButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (tempEdition != null) {
	       int i = JOptionPane.showConfirmDialog(DialogEditionsMain.this, "Remove Edition\nConfirm?", "Remove Edition", JOptionPane.YES_NO_OPTION, JOptionPane.YES_NO_OPTION);
	       if (i == JOptionPane.YES_OPTION) {
		  OlympicGamesGui.ap.removeEdition(tempEdition);
		  editions.remove(tempEdition);
		  refreshTable();
		  JOptionPane.showMessageDialog(DialogEditionsMain.this, "Edition Removed");
	       }
	    } else {
	       JOptionPane.showMessageDialog(DialogEditionsMain.this, "No Edition Selected");
	    }
	 }
      });
      editButton = new JButton("Edit", new ImageIcon(cldr.getResource("images/extra/page_edit.png")));
      editButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (tempEdition != null) {
	       DialogManageEdition manEd = new DialogManageEdition(DialogEditionsMain.this, ("Manage " + tempEdition.getYear() + " Edition"), tempEdition, DialogEditionsMain.this);
	       manEd.showJan();
	    } else {
	       JOptionPane.showMessageDialog(DialogEditionsMain.this, "No Edition Selected");
	    }
	 }
      });

      manEventsButton = new JButton("Manage Events", new ImageIcon(cldr.getResource("images/extra/page_edit.png")));
      manEventsButton.addActionListener(new ActionListener() {

	 @Override
	 public void actionPerformed(ActionEvent e) {
	    if (tempEdition == null) {
	       JOptionPane.showMessageDialog(DialogEditionsMain.this, "No Edition Selected");
	    } else {
	       if (!OlympicGamesGui.ap.getEditions().isEmpty()) {
		  DialogManageEditionEvents mev = new DialogManageEditionEvents(DialogEditionsMain.this, "teste", tempEdition);
		  mev.showJan();
	       }
	    }
	 }
      });

      pMbt.add(newButton);
      pMbt.add(deleteButton);
      pMbt.add(editButton);
      pMbt.add(manEventsButton);

      pMod.add(pMbt, BorderLayout.NORTH);

      tabela1 = new JTable() {

	 public boolean isCellEditable(int rowIndex, int colIndex) {
	    return false; //Disallow the editing of any cell
	 }
      };
      TableModel = (DefaultTableModel) tabela1.getModel();
      TableModel.setColumnIdentifiers(columnNames);
      setUpTableData();
      JTableHeader header = tabela1.getTableHeader();
      header.setBackground(Color.DARK_GRAY);
      header.setFont(new Font("Arial", Font.BOLD, 12));
      header.setForeground(Color.WHITE);
      tabela1.setPreferredScrollableViewportSize(new Dimension(300, 70));
      tabela1.setFillsViewportHeight(true);
      tabela1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      tabela1.getSelectionModel().addListSelectionListener(new DialogEditionsMain.RowListener());
      tabela1.getTableHeader().setReorderingAllowed(false);




      scroller = new JScrollPane(tabela1);
      pMod.add(scroller, BorderLayout.CENTER);
      pCentro = new JPanel(new BorderLayout());
      pCentro.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
      pCentro.add(pMbt, BorderLayout.NORTH);
      pCentro.add(pMod, BorderLayout.CENTER);
      add(pCentro, BorderLayout.CENTER);


      setMinimumSize(new Dimension(450, 400));
      pack();
      setVisible(true);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      addWindowListener(new WindowAdapter() {

	 @Override
	 public void windowClosing(WindowEvent e) {
	    dispose();
	 }
      });





      addWindowListener(new WindowAdapter() {

	 @Override
	 public void windowClosing(WindowEvent e) {
	    dispose();
	 }
      });
   }

   private class RowListener implements ListSelectionListener {

      @Override
      public void valueChanged(ListSelectionEvent event) {
	 if (event.getValueIsAdjusting()) {
	    return;
	 }
	 lineSelected = tabela1.getSelectedRow();
	 if (lineSelected == -1) {
	    lineSelected = 0;
	 } else {
	    tempEdition = editions.getElement(lineSelected);
	    System.out.println(tempEdition.getYear());
	 }
      }
   }

   private void setUpTableData() {
      Object[][] data1 = new Object[editions.getSize()][columnNames.length];
      Collections.sort(editions);
      for (int j = 0; j < editions.size(); j++) {
	 Edition e = editions.getElement(j);
	 data1[j][0] = e.getYear();
	 data1[j][1] = e.getCity();
	 data1[j][2] = e.getOfficialDesignation();
	 data1[j][3] = e.getEditionDelegationList().size();
	 data1[j][4] = e.getEditionDisciplineEventList().size();
	 TableModel.addRow(data1[j]);

      }
      tabela1.setModel(TableModel);

   }

   public void refreshTable() {
      TableModel.setRowCount(0);
      tabela1.removeAll();
      setUpTableData();
   }
}